#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
主题: 展开嵌套的序列
问题: 你想将一个多层嵌套的序列展开成一个单层列表
提示 : 
    1. yield from 在涉及到基于协程和生成器的并发编程中扮演着更加重要的角色
"""

from collections.abc import Iterable


def recipe1():
    items = [1, 2, [3, 4, [5, 6], 7], 'ab', 8]
    for x in flatten(items):
        print(f'{x = }')


def recipe2():
    items = ['Dave', 'Paula', ['Thomas', 'Lewis']]
    for x in flatten(items):
        print(f'{x = }')


def flatten(items, ignore_type=(str, bytes)):
    for x in items:
        if isinstance(x, Iterable) and not isinstance(x, ignore_type):
            yield from flatten(x)
        else:
            yield x


def main():
    print('recipe1'.center(20, '*'))
    recipe1()
    print('recipe2'.center(20, '*'))
    recipe2()

if __name__ == '__main__':
    main()
